package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
)

func FileDownload(ctx *gin.Context) {
	fileName := ctx.Param("filename")
	fmt.Println("下载的文件 ", fileName)

	_, err := ioutil.ReadFile("F:/wordFile/" + fileName)
	if err != nil {
		ctx.String(http.StatusNotFound, fmt.Sprintf("文件不存在!"))
		return
	}
	filePath := fmt.Sprintf("F:/wordFile/%s", fileName)

	// 设置响应头信息
	ctx.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
	ctx.Writer.Header().Add("Content-Type", "application/octet-stream")
	ctx.File(filePath)
}

func main() {
	router := gin.Default()
	router.GET("/download/:filename", FileDownload)
	_ = router.Run(":8080")

}
